<?php
namespace app\index\controller;
use app\model\Activity as ActivityModel;

use think\Db;


class Activity extends Base
{   
    protected $noCheckToken = ['index','read','recommend'];

    // 列表
    public function index(){
        $map_params = [
           ['key'=>'category_uuid','type'=>'='],
           ['key'=>'tags','type'=>'like'],
        ];
        $map = getSearchParam($map_params);
        if (isSearchParam('keyword_search')) {
            $map[] = ['name','like','%'.input('keyword_search').'%'];
        }
        if (input('is_collect',0) == 1) {
            $user_uuid = $this->login_user['uuid'];
            $uuids = Db::name('collect')->where([
                ['user_uuid','=',$user_uuid],
                ['type','=',2]
            ])->column('target_uuid');
            $map[] = ['uuid','in',$uuids];
        }
        if (isSearchParam('start_time')) {
            $map[] = ['activity_date','>=',input('start_time')];
        }
        if (isSearchParam('end_time')) {
            $map[] = ['activity_date','<=',input('end_time')];
        }
        $map[] = ['status','=',1];
        
        $model = new ActivityModel();
        $page_param = ['page'=>input('page_index',1),'list_rows'=>input('page_size',10)];
        $list = $model->where($map)->order('create_time','desc')->paginate($page_param)->toarray();
        foreach ($list['data'] as $k => $vo) {
            $list['data'][$k]['tags'] = myExplode($vo['tags']);
            $list['data'][$k]['category'] = Db::name('category')->field('name')->where('uuid',$vo['category_uuid'])->find();
            $list['data'][$k]['is_valid'] = strtotime($vo['valid_time']) > time() && $vo['stock'] > $vo['buy_num'] ? 1 : 0;

            $list['data'][$k]['classroom'] = Db::name('classroom')->where('uuid',$vo['classroom_uuid'])->find();
        }

        return apiResult('2000','获取成功',$list);
    }

    // 详情
    public function read($uuid){
        $model = new ActivityModel();
        $data = $model->where('uuid',$uuid)->find();
        $data['tags'] = myExplode($data['tags']);
        $data['images'] = myExplode($data['images']);
        $data['category'] = Db::name('category')->field('name')->where('uuid',$data['category_uuid'])->find();

        $user_uuid = $this->login_user['uuid'];
        $data['is_collect'] = Db::name('collect')->where([
            ['user_uuid','=',$user_uuid],
            ['target_uuid','=',$data['uuid']],
            ['type','=',2]
        ])->count() > 0 ? 1 : 0;
   
        $data['is_valid'] = strtotime($data['valid_time']) > time() && $data['stock'] > $data['buy_num'] ? 1 : 0;

        $data['classroom'] = Db::name('classroom')->where('uuid',$data['classroom_uuid'])->find();

        $djxx_field = Db::name('djxx_field')->where('uuid',$data['djxx_field_uuid'])->find();
        $djxx_field['fields'] = json_decode($djxx_field['fields'],true);
        $data['djxx_field'] = $djxx_field;


        return apiResult('2000','获取成功',$data);
    }

    // 推荐
    public function recommend(){
        $map_params = [
           ['key'=>'category_uuid','type'=>'='],
        ];
        $map = getSearchParam($map_params);
        if (isSearchParam('keyword_search')) {
            $map[] = ['name','like','%'.input('keyword_search').'%'];
        }
        if (isSearchParam('uuid')) {
            $map[] = ['uuid','<>',input('uuid')];
        }
        $map[] = ['status','=',1];

        $model = new ActivityModel();
        $list = $model->field('content,status',true)->where($map)->orderRaw('rand()')->limit(10)->select()->toarray();
        foreach ($list as $k => $vo) {
            $list[$k]['tags'] = myExplode($vo['tags']);
            $list[$k]['category'] = Db::name('category')->field('name')->where('uuid',$vo['category_uuid'])->find();
        }

        return apiResult('2000','获取成功',$list);
    }

    // 收藏
    public function collect($uuid){
        $user_uuid = $this->login_user['uuid'];
        $map = [
            ['user_uuid','=',$user_uuid],
            ['target_uuid','=',$uuid],
            ['type','=',2]
        ];
        if (Db::name('collect')->where($map)->count() > 0) {
            Db::name('collect')->where($map)->delete();
            return apiResult('2000','取消收藏成功',['type'=>0]);
        }else{
            Db::name('collect')->insert([
                'user_uuid'=>$user_uuid,
                'target_uuid'=>$uuid,
                'type'=>2,
                'create_time'=>date('Y-m-d H:i:s')
            ]);
            return apiResult('2000','收藏成功',['type'=>1]);
        }
    }



}
